<?php
# get_infos.php?type=[all|address|award|biography|general_information|product|q_a|recipe|song]&episode_id=$episode&selected=json obj

require 'TVB/pms/pdes/common/bootstrap.php';

$info_type = array( 'all','address','award','biography','general_info','product','qa','recipe','song','pageant');

if(empty($_REQUEST['episode_id'])){
	exit('episode_id is null');
}

if(empty($_REQUEST['infotype']) || !in_array($_REQUEST['infotype'],$info_type)){
	exit('infotype is error'); 
}

if(empty($_REQUEST['selected']) || !($selected = json_decode($_REQUEST['selected'],1))){
	$selected = $info_type;
}

$episode_id   = $_REQUEST['episode_id'];
$current_type = $_REQUEST['infotype'];

$address_sql             = 'select id,"address" as type,concat(venue_name_chinese," ",venue_name_english) as title from info_address where episode_id = :id  and status !=2';
$award_sql               = 'select id,"award" as type, concat(award_title_chinese," ",award_title_english) as title from info_award where episode_id = :id  and status !=2';
$biography_sql           = 'select id,"biography" as type, concat(name_chinese," ",name_english) as title from info_biography where episode_id = :id  and status !=2';
$general_info_sql        = 'select id,"general_info" as type, concat(title_chinese," ",title_english) as title from info_general_information where episode_id = :id  and status !=2';
$pageant_sql             = 'select id,"pageant" as type, concat(name_chinese," ",name_english) as title from info_pageant where episode_id = :id  and status !=2';
$product_sql             = 'select id,"product" as type, concat(product_name_chinese," ",product_name_english) as title from info_product where episode_id = :id  and status !=2';
$qa_sql                  = 'select id,"qa" as type, question as title from info_q_a where episode_id = :id  and status !=2';
$recipe_sql              = 'select id,"recipe" as type, concat(dish_name_chinese," ",dish_name_english) as title from info_recipe where episode_id = :id and status !=2';
$song_sql                = 'select id,"song" as type, concat(title_chinese," ",title_english) as title from info_song where episode_id = :id  and status !=2';

if('all' == $current_type)
{
	unset($info_type[0]);
	$sql_array = array();
	foreach($info_type as $type)
	{
		$sql_str = $type.'_sql';
		$sql_array[] = $$sql_str;
	}
	$sql = implode(' union all ', $sql_array);
}
else
{
	if(!isset($selected[$current_type])){
		$selected[$current_type] = $selected;
	}
	$sql_str = $current_type.'_sql';
	$sql     = $$sql_str;
}

$rs = Model::selectBySql($sql,array(':id'=>$episode_id));

foreach($rs as $r)
{
	$checked_str = (isset($selected[$r['type']]) && in_array($r['id'],$selected[$r['type']])) ? 'checked=checked' : '';

?>
	<div class="infotype_result form-item">
		<p><?= htmlentities($r['title'], ENT_QUOTES, 'UTF-8'); ?></p>
		<a class="infotype_details ui-icon ui-icon-search" href="pms_partial.html?act=view&type=<?= $r['type'] ?>&id=<?= $r['id'] ?>" ></a>
		<input type="checkbox" value="<?= $r['type'] ?>:<?= $r['id'] ?>" <?= $checked_str ?>/>
	</div>

<?php } ?>
